<?php
	class login_regControl extends Control{
		//用户登陆视图
		public function login(){
			indexControl::get_citys_view();
			$this->display();
		}
		//处理用户登陆
		public function login_exe(){
			$db = M("user");
			$where = $_POST['uname'];
			$pwd = md5($_POST['pwd']);
			$user = $db->where("username = '".$where."' or email = '".$where."' or mobile = '".$where."' ")->find();
			if($user){
				if($pwd == $user['password']){
					$_SESSION['user'] = $user;
					go(__WEB__.'/index/user/orderlist');
				}else{
					$this->error('密码错误');
				}
			}else{
				$this->error('用户不存在');
			}
			
		}
		//用户登出
		public function loginout(){
			$this->session_delall();
			go(__WEB__);
		}
		//用户注册
		public function reg(){
			indexControl::get_citys_view();
			$this->display();
		}
		//处理用户注册
		public function reg_exe(){
			if(strtolower($_POST['code']) != strtolower($_SESSION['code'])){
				$this->error('验证码不正确');
			}else{
				$db = M("user");
				$db->validata = array(
						array('name','nonull','用户名不能为空',2),
						array('pwd','nonull','密码不能为空',2),
						array('pwd','regexp:/^[a-zA-Z0-9_@!\$%\^~]{6,18}$/',"密码长度为6-18位",2),
						array('repwd','nonull','重复密码不能为空',2),
						array('pwd','confirm:repwd','两次密码不相同',2),
						array('email','nonull','邮箱不能为空',2),
						array('email','email','邮箱格式不正确',2),
						array('mnum','phone','手机号格式不正确',3),
					);
				if(!$db->validate()){
					$this->error($db->error);
				}else{
					$_POST['password'] = md5($_POST['pwd']);
					$_POST['username'] = $_POST['name'];
					$_POST['mobile'] = $_POST['mnum'];
					
					/*$db->map = array(
							'name'=>'username',
							'pwd'=>'password',
							'mnum'=>'mobile',
						);*/
					$_POST['regtime'] = time();
					$_POST['isadmin'] = 0;
					$_POST['regip'] = ip_get_client();
					
					if($db->add($_POST)){
						go(__CONTROL__.'/login');
					}else{
						$this->error("注册失败");
					}
				}
			}
			
		}
		/**
		 * 用户注册时验证用户名 邮箱 手机号码
		 */
		public function check(){
			C('debug',0);
			if($_POST['name'] == 'name'){
				$name = 'username';
				$msg = '用户名已经存在';
			}elseif($_POST['name'] == 'email'){
				$name='email';
				$msg = '邮箱已经注册过了';
			}elseif($_POST['name'] == 'mnum'){
				$name='mobile';
				$msg = '手机号码已经注册了';
			}
			$value = $_POST['param'];
			$user = M('user');
			$row = $user->where(" ".$name."='" .$value."'")->find();
			if($row){
				echo $msg;
			}else{
				echo 'y';
			}
		}
		public function code(){
			$code = new code('95','28','','',4,18);
			$code->show();
		}
	}

?>